import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { ExtendInterfaceLogModel } from '#/api/basic/msg/model/extendInterfaceLogModel';
import type { ActionEnum } from '#/enums/commonEnum';

import { ref } from 'vue';
import { useRouter } from 'vue-router';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { dict } from '@fast-crud/fast-crud';

import { DefTenantApi } from '#/api';
import { ExtendInterfaceLogApi } from '#/api/basic/msg/extendInterfaceLog';
import { RouteEnum } from '#/enums/commonEnum';
import { PermCode } from '#/enums/perm';
import {
  createdTimeColumn,
  deleteButton,
  indexColumn,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.devOperation.ops.defInterfaceLog;

export function createCrudOptions(
  props: CreateCrudOptionsProps<
    ExtendInterfaceLogModel.ExtendInterfaceLogResultVO,
    any
  >,
): CreateCrudOptionsRet<ExtendInterfaceLogModel.ExtendInterfaceLogResultVO> {
  const { push } = useRouter();

  const selectedRowKeys = ref<string[]>([]);

  return {
    crudOptions: {
      request: {
        pageRequest: ExtendInterfaceLogApi.pageRequest,
        addRequest: ExtendInterfaceLogApi.addRequest,
        editRequest: ExtendInterfaceLogApi.editRequest,
        delRequest: ExtendInterfaceLogApi.delRequest,
        infoRequest: ExtendInterfaceLogApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: { show: false },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            role: permCode.delete,
            removeFn: ExtendInterfaceLogApi.remove,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: { show: false },
          view: {
            show: hasPermission(permCode.logging),
            click({ row }) {
              const searchData = props.crudExpose.getSearchFormData();
              push({
                name: RouteEnum.BASIC_MSG_INTERFACE_LOGGING,
                params: { id: row.id },
                query: { tenantId: searchData?.tenantId },
              });
            },
          },
          remove: {
            show: hasPermission(permCode.delete),
          },
          copy: { show: false },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        tenantId: {
          title: '所属企业',
          type: 'dict-select',
          dict: dict({
            value: 'id',
            label: 'name',
            async getData() {
              return await DefTenantApi.query({});
            },
          }),
          search: { show: true },
          form: { show: false },
          column: { show: false },
        },
        name: {
          title: $t('basic.msg.extendInterfaceLog.name'),
          type: 'text',
          search: { show: true },
        },
        successCount: {
          title: $t('basic.msg.extendInterfaceLog.successCount'),
          type: 'text',
        },
        failCount: {
          title: $t('basic.msg.extendInterfaceLog.failCount'),
          type: 'text',
        },
        lastExecTime: {
          title: $t('basic.msg.extendInterfaceLog.lastExecTime'),
          type: 'datetime',
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
